package com.yakaten.logic;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.log4j.Logger;
import org.seasar.extension.jdbc.SqlBatchUpdate;
import org.seasar.extension.jdbc.SqlUpdate;
import org.seasar.framework.beans.util.BeanMap;

import com.yakaten.dto.YahooPriceTaskDto;
import com.yakaten.exception.SystemException;
import com.yakaten.service.JdbcManagerFactory;

/**
 *
 *
 */
public class YahooPriceLogic extends JdbcManagerFactory {

	private static final String TABLE_NAME = "yahoo_task";

	static Logger logger = Logger.getLogger(YahooPriceLogic.class);

	/**
	 *
	 * @return
	 * @throws SystemException
	 */
	public List<YahooPriceTaskDto> getAllTaskList() throws SystemException {
		List<YahooPriceTaskDto> resultList = new ArrayList<YahooPriceTaskDto>();

		List<BeanMap> result = jdbcManager.selectBySql(BeanMap.class,
				"SELECT * FROM " + TABLE_NAME).getResultList();

		for (BeanMap current : result) {
			YahooPriceTaskDto dto = new YahooPriceTaskDto();
			dto.setTaskNo((String) current.get("taskno"));
			dto.setYahooItemId((String) current.get("yahooitemid"));
			dto.setYahooItemName((String) current.get("yahooitemname"));
			dto.setYahooItemPrice(((BigDecimal) current.get("yahooitemprice")).longValue());
			dto.setYahooItemCategory((String) current.get("yahooitemcategory"));

			dto.setShopItemId((String) current.get("shopitemid"));
			dto.setShopItemName((String) current.get("shopitemname"));
			dto.setShopItemPrice(((BigDecimal) current.get("shopitemprice"))
					.longValue());
			dto.setStartPrice(((BigDecimal) current.get("startprice"))
					.longValue());
			dto.setMinPrice(((BigDecimal) current.get("minprice")).longValue());
			dto.setStartTime((Date) current.get("starttime"));
			dto.setStatus(Long.parseLong((String) current.get("status")));
			dto.setQuantity(""
					+ ((BigDecimal) current.get("quantity")).longValue());
			dto.setComparePrice(((BigDecimal) current.get("compareprice")).longValue());
			dto.setComparePricePer(((BigDecimal) current.get("comparepriceper")).longValue());
			resultList.add(dto);
		}
		return resultList;
	}

	/**
	 *
	 * @param yahooItemId
	 * @param shopItemId
	 * @return
	 * @throws SystemException
	 */
	public boolean isExistTask(String yahooItemId, String shopItemId)
			throws SystemException {
		boolean isExit = false;

		List<BeanMap> result = jdbcManager.selectBySql(
				BeanMap.class,
				"SELECT * FROM " + TABLE_NAME
						+ " where yahooItemId = ? and shopItemId = ?",
				yahooItemId, shopItemId).getResultList();

		if (result != null && result.size() > 0) {
			isExit = true;
		}

		return isExit;
	}

	/**
	 *
	 * @return
	 * @throws SystemException
	 */
	public String getNewTaskNo() throws SystemException {
		String newTaskNo = "1";

		BeanMap result = jdbcManager.selectBySql(
				BeanMap.class,
				"SELECT max(cast(taskNo as SIGNED)) +1 newTaskNo FROM "
						+ TABLE_NAME).getSingleResult();

		if (result != null && result.get("newtaskno") !=null) {
			newTaskNo = ((Long) result.get("newtaskno")).toString();
		}

		return newTaskNo;
	}

	/**
	 *
	 * @param dto
	 * @return
	 * @throws SystemException
	 */
	public boolean addTask(YahooPriceTaskDto dto) throws SystemException {
		boolean result = false;
		String sql = "INSERT INTO " + TABLE_NAME
				+ " VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
		SqlUpdate batchUpdate = jdbcManager.updateBySql(sql, String.class,
				String.class, String.class, Integer.class, String.class, String.class,
				String.class, Integer.class, Integer.class, Integer.class,
				Date.class, String.class, Integer.class,  Integer.class, Integer.class);
		batchUpdate.params(dto.getTaskNo(), dto.getYahooItemId(), dto
				.getYahooItemName(), dto.getYahooItemPrice(), dto.getYahooItemCategory(),dto
				.getShopItemId(), dto.getShopItemName(),
				dto.getShopItemPrice(), dto.getStartPrice(), dto.getMinPrice(),
				dto.getStartTime(), dto.getStatus(), dto.getQuantity(), dto.getComparePrice(),dto.getComparePricePer());

		try {
			int re = batchUpdate.execute();
			if (re == 1) {
				result = true;
			}
		} catch (Exception e) {
			throw new SystemException(e);
		}

		return result;
	}

	/**
	 *
	 * @param dataList
	 * @return
	 * @throws SystemException
	 */
	public boolean updateTask(List<YahooPriceTaskDto> dataList)
			throws SystemException {
		boolean result = false;

		String sql = "update " + TABLE_NAME + " set " + "yahooItemId = ? "
				+ ",yahooItemName = ? " + ",yahooItemPrice = ? "+ ",yahooItemCategory = ? "
				+ ",shopItemId = ? " + ",shopItemName = ? "
				+ ",shopItemPrice = ? " + ",startPrice = ? " + ",minPrice = ? "
				+ ",startTime = ? " + ",status = ? " + ",quantity = ? "
				+  ",comparePrice = ? " +  ",comparePricePer = ? " + "where taskNo = ?";

		SqlBatchUpdate batchUpdate = jdbcManager.updateBatchBySql(sql,
				String.class, String.class, Integer.class, String.class,String.class,
				String.class, Integer.class, Integer.class, Integer.class,
				Date.class, String.class, Integer.class, Integer.class,Integer.class,
				String.class);

		for (YahooPriceTaskDto current : dataList) {
			batchUpdate.params(current.getYahooItemId(), current
					.getYahooItemName(), current.getYahooItemPrice(), current.getYahooItemCategory(), current
					.getShopItemId(), current.getShopItemName(), current
					.getShopItemPrice(), current.getStartPrice(), current
					.getMinPrice(), current.getStartTime(),
					current.getStatus(), current.getQuantity(), current.getComparePrice(),current.getComparePricePer(),current.getTaskNo());
		}

		try {
			int[] res = batchUpdate.execute();
			result = true;
		} catch (Exception e) {
			throw new SystemException(e);
		}

		return result;
	}

	/**
	 *
	 * @param updateDto
	 * @return
	 * @throws SystemException
	 */
	public boolean updateTask(YahooPriceTaskDto updateDto)
			throws SystemException {
		boolean result = false;

		String sql = "update " + TABLE_NAME + " set " + "yahooItemId = ? "
		+ ",yahooItemName = ? " + ",yahooItemPrice = ? "+ ",yahooItemCategory = ? "
		+ ",shopItemId = ? " + ",shopItemName = ? "
		+ ",shopItemPrice = ? " + ",startPrice = ? " + ",minPrice = ? "
		+ ",startTime = ? " + ",status = ? " + ",quantity = ? "
		+  ",comparePrice = ? " +  ",comparePricePer = ? " + "where taskNo = ?";

		SqlUpdate sqlUpdate = jdbcManager.updateBySql(sql,
				String.class, String.class, Integer.class, String.class,String.class,
				String.class, Integer.class, Integer.class, Integer.class,
				Date.class, String.class, Integer.class, Integer.class,Integer.class,
				String.class);

		sqlUpdate.params(updateDto.getYahooItemId(), updateDto
				.getYahooItemName(), updateDto.getYahooItemPrice(), updateDto.getYahooItemCategory(), updateDto
				.getShopItemId(), updateDto.getShopItemName(), updateDto
				.getShopItemPrice(), updateDto.getStartPrice(), updateDto
				.getMinPrice(), updateDto.getStartTime(),
				updateDto.getStatus(), updateDto.getQuantity(), updateDto.getComparePrice(),updateDto.getComparePricePer(),updateDto.getTaskNo());

		try {
			int res = sqlUpdate.execute();
			if (res == 1) {
				result = true;
			}

		} catch (Exception e) {
			throw new SystemException(e);
		}

		return result;
	}

	/**
	 *
	 * @param taskNo
	 * @return
	 * @throws SystemException
	 */
	public boolean deleteTask(String taskNo)
			throws SystemException {
		boolean result = false;

		String sql = "delete from " + TABLE_NAME + " where taskNo = ?";

		SqlUpdate sqlUpdate = jdbcManager.updateBySql(sql, String.class);

		sqlUpdate.params(taskNo);

		try {
			int res = sqlUpdate.execute();
			if (res == 1) {
				result = true;
			}

		} catch (Exception e) {
			throw new SystemException(e);
		}

		return result;
	}

	/**
	 *
	 * @param taskNo
	 * @param status
	 * @return
	 * @throws SystemException
	 */
	public boolean updateTaskStatus(String taskNo, long status)
			throws SystemException {
		boolean result = false;

		String sql = "update " + TABLE_NAME
				+ " set  status = ?  where taskNo = ?";

		SqlUpdate sqlUpdate = jdbcManager.updateBySql(sql, String.class,
				String.class);

		sqlUpdate.params(status, taskNo);

		try {
			int res = sqlUpdate.execute();
			if (res == 1) {
				result = true;
			}

		} catch (Exception e) {
			throw new SystemException(e);
		}

		return result;
	}

	public static void main(String[] args) {
		try {

			YahooPriceLogic logic = new YahooPriceLogic();

			YahooPriceTaskDto dto = new YahooPriceTaskDto();
			dto.setTaskNo("1");
			dto.setYahooItemId("4548736712799--");
			dto.setYahooItemName("SONY ");
			dto.setYahooItemPrice(115500);
			dto.setYahooItemCategory("a");

			dto.setShopItemId("005001000249");
			dto.setShopItemName("SONY ");
			dto.setShopItemPrice(116855);
			dto.setStartPrice(116855);
			dto.setMinPrice(1);
			dto.setStartTime(new Date());
			dto.setStatus(1);
			dto.setQuantity("11");
			dto.setComparePrice(1);
			dto.setComparePricePer(11);
//			System.out.println(logic.addTask(dto));
//			 System.out.println(logic.getNewTaskNo());
			// System.out.println(logic.getAllTaskList().get(0));
			// System.out.println(logic.isExistTask("K0000305715","0050060000281"));
			// System.out.println(logic.getNewTaskNo());

			YahooPriceTaskDto dto1 = new YahooPriceTaskDto();
			dto1.setTaskNo("1");
			dto1.setYahooItemId("-K0000305715");
			dto1.setYahooItemName("-MacBook Pro 2400/15.4 MD322J/A");
			dto1.setYahooItemPrice(777);
			dto1.setShopItemId("-05006000028");
			dto1.setShopItemName("-MacBook]");
			dto1.setShopItemPrice(888);
			dto1.setStartPrice(888);
			dto1.setMinPrice(888);
			dto1.setStartTime(new Date());
			dto1.setStatus(1);
			dto1.setQuantity("788877");
			dto1.setComparePrice(-4);
			dto1.setComparePricePer(14);

			List<YahooPriceTaskDto> updataList = new ArrayList<YahooPriceTaskDto>();
			updataList.add(dto1);
			 System.out.println(logic.updateTask(updataList));
//			System.out.println(logic.updateTaskStatus("3", 9));

//			System.out.println("get new Task No > "+ logic.getNewTaskNo());
//			System.out.println("add task > "+logic.addTask(dto));
//			System.out.println("isExistTask > "+logic.isExistTask("4548736712799--", "005001000249"));
//			System.out.println("getAllTaskList > "+logic.getAllTaskList().get(0));

//			YahooPriceTaskDto dto1 = new YahooPriceTaskDto();
//			dto1.setTaskNo("1");
//			dto1.setYahooItemId("4548736712799--a");
//			dto1.setYahooItemName("SONY VAIO Duo 13 SVD13219CJB [ブラック][正規版Office搭載]a");
//			dto1.setYahooItemPrice(1155009);
//			dto1.setYahooItemCategory("パソコン:ノートパソコン:SONYa");
//
//			dto1.setShopItemId("005001000249a");
//			dto1.setShopItemName("SONY VAIO Duo 13 SVD13219CJB [ブラック]a");
//			dto1.setShopItemPrice(1168559);
//			dto1.setStartPrice(1168559);
//			dto1.setMinPrice(19);
//			dto1.setStartTime(new Date());
//			dto1.setStatus(3);
//			dto1.setQuantity("119");
//			dto1.setComparePrice(19);
//			List list = new ArrayList();
//			list.add(dto1);
//			System.out.println(logic.updateTaskStatus("1", 2));
			System.out.println("getAllTaskList > "+logic.getAllTaskList().get(0));

		} catch (SystemException e) {
			e.printStackTrace();
		}
	}
}
